package warmachineroster.grapheditor.dnd;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.dnd.AbstractTransferDragSourceListener;
import org.eclipse.swt.dnd.DragSourceEvent;

import warmachinesystem.BaseUnit;

public class OutlineDragSourceListener extends
		AbstractTransferDragSourceListener {

	public OutlineDragSourceListener(EditPartViewer viewer) {
		super(viewer, OutlineToRosterTransfer.getInstance());
	}

	private List<BaseUnit> transferData;
	
	@Override
	public void dragSetData(DragSourceEvent event) {
		event.data = getSelection();
	}
	
	@Override
	public void dragFinished(DragSourceEvent event) {
		transferData = null;
		OutlineToRosterTransfer.getInstance().setObject(null);
	}
	
	public void dragStart(DragSourceEvent event) {
		List<BaseUnit> data = getSelection();
//		if (data.isEmpty())
//			event.doit = false;
		OutlineToRosterTransfer.getInstance().setObject(data);
	}
	
	protected List<BaseUnit> getSelection() {
		List<?> selection = getViewer().getSelectedEditParts();
//		System.out.println("selection: " + selection);
		transferData = new ArrayList<BaseUnit>();
		for (int i = 0; i < selection.size(); i++) {
			transferData.add((BaseUnit) ((EditPart)selection.get(i)).getModel());
		}
		return transferData;
	}

}
